import threading
import time
from datetime import datetime

from ttkbootstrap.constants import *

from app.view.dev_tool.abin_view import AbinView
from app.view.dev_tool.ble_connector_view import BleConnectorView
from app.view.dev_tool.lib_parser_view import LibParserView
from app.view.dev_tool.py_code_view import PyCodeView
from app.view.dev_tool.serial_cmd_view import SerialCmdView
from app.view.dev_tool.ssh_view import SshView
from app.view.file.binary_view import BinaryView
from app.view.file.file_view import FileView
from app.view.file.home_view import HomeView
from app.view.file.pdf_view import PdfView
from app.view.file.text_view import TextView
from app.view.ft_tool.ft_au_card_cmd_config_view import FtAuCardCmdConfigView
from app.view.ft_tool.ft_au_card_config_view import FtAuCardConfigView
from app.view.ft_tool.ft_au_sn_print_view import FtAuSnPrintView
from app.view.ft_tool.ft_jlink_view import FtJlinklView
from app.view.ft_tool.ft_serial_hw_au_view import FtSeriaHwAulView
from app.view.ft_tool.ft_serial_update_view import FtSerialUpdateView
from app.view.ft_tool.ft_serial_ymodem_update_view import FtSerialYmodemUpdateView
from app.view.ft_tool.ft_tcp_ble_hw_au_view import FtTcpBleHwAulView
from app.view.ft_tool.ft_tcp_update_view import FtTcpUpdateView
from app.view.ft_tool.ft_udp_cmd_config_view import FtUdpConfigView
from app.view.help.about_view import AboutView
from app.view.muse.music_view import MusicView
from app.view.muse.video_view import VideoView
from app.view.setting.settings_view import SettingsView
from app.view.tool.api_view import ApiView
from app.view.tool.check_sum_view import CheckSumView
from app.view.tool.hex_bin_view import HexBinView
from app.view.tool.mac_generator_view import MacGeneratorView
from app.view.tool.net_speed_view import NetSpeedView
from app.view.tool.net_view import NetView
from app.view.tool.serial_view import SerialView
from app.view.tool.task_view import TaskView


class MainController:
    """主控制器，管理视图切换和状态更新"""

    def __init__(self, main_window, view_model):
        self.main_window = main_window
        self.current_view = None
        self.views = {}

        # 初始化所有视图
        self._initialize_views(view_model)

        # 显示初始视图
        self.show_view("home")

        # 启动时间更新线程
        self._start_time_updater()

    def _initialize_views(self, view_model):
        # view_model生成页 在打包环节会出问题
        # for cls in view_model:
        #     # 类名称:cls.__name__ 模块:cls.__module__
        #     self.views[cls.__name__] = cls(self.main_window.content_frame, self)
        """查看视图"""
        self.views["home"] = HomeView(self.main_window.content_frame, self)
        self.views["file_text"] = TextView(self.main_window.content_frame, self)
        self.views["file_binary"] = BinaryView(self.main_window.content_frame, self)
        self.views["file_pdf"] = PdfView(self.main_window.content_frame, self)
        # self.views["file_cad"] = CadView(self.main_window.content_frame, self)
        self.views["file_sys"] = FileView(self.main_window.content_frame, self)

        """设置视图"""
        self.views["settings"] = SettingsView(self.main_window.content_frame, self)

        """工具"""
        self.views["serial"] = SerialView(self.main_window.content_frame, self)
        self.views["net"] = NetView(self.main_window.content_frame, self)
        self.views["check_sum"] = CheckSumView(self.main_window.content_frame, self)
        self.views["net_speed"] = NetSpeedView(self.main_window.content_frame, self)
        # self.views["remote_desktop"] = RemoteDesktopView(self.main_window.content_frame, self)
        # self.views["calculate"] = CalculateView(self.main_window.content_frame, self)
        # self.views["3d"] = TdView(self.main_window.content_frame, self)
        self.views["api"] = ApiView(self.main_window.content_frame, self)
        self.views["hex_bin"] = HexBinView(self.main_window.content_frame, self)
        self.views["task"] = TaskView(self.main_window.content_frame, self)
        # self.views["tcp_file_transfer"] = TcpFileTransferView(self.main_window.content_frame, self)
        # self.views["img_convertor"] = ImgConvertorView(self.main_window.content_frame, self)
        # self.views["img_edit"] = ImgEditView(self.main_window.content_frame, self)
        self.views["mac_generator"] = MacGeneratorView(self.main_window.content_frame, self)

        """FT工具"""
        self.views["ft_jlink"] = FtJlinklView(self.main_window.content_frame, self)
        self.views["ft_serial_hw_au"] = FtSeriaHwAulView(self.main_window.content_frame, self)
        self.views["ft_tcp_ble_hw_au"] = FtTcpBleHwAulView(self.main_window.content_frame, self)
        self.views["ft_serial_update"] = FtSerialUpdateView(self.main_window.content_frame, self)
        self.views["ft_tcp_update"] = FtTcpUpdateView(self.main_window.content_frame, self)
        # self.views["ft_serial_param_set"] = FtSerialParamSetlView(self.main_window.content_frame, self)
        self.views["ft_serial_ymodem_update"] = FtSerialYmodemUpdateView(self.main_window.content_frame, self)
        self.views["ft_au_card_config"] = FtAuCardConfigView(self.main_window.content_frame, self)
        self.views["ft_au_card_cmd_config"] = FtAuCardCmdConfigView(self.main_window.content_frame, self)
        self.views["ft_au_sn_print"] = FtAuSnPrintView(self.main_window.content_frame, self)
        self.views["ft_udp_config"] = FtUdpConfigView(self.main_window.content_frame, self)

        """DEV工具"""
        self.views["py_code"] = PyCodeView(self.main_window.content_frame, self)
        self.views["ssh"] = SshView(self.main_window.content_frame, self)
        self.views["ble_connector"] = BleConnectorView(self.main_window.content_frame, self)
        self.views["abin"] = AbinView(self.main_window.content_frame, self)
        self.views["serial_cmd"] = SerialCmdView(self.main_window.content_frame, self)
        self.views["lib_parser"] = LibParserView(self.main_window.content_frame, self)

        """MUSE 娱乐工具"""
        self.views["music"] = MusicView(self.main_window.content_frame, self)
        self.views["video"] = VideoView(self.main_window.content_frame, self)

        """帮助视图"""
        self.views["about"] = AboutView(self.main_window.content_frame, self)

    def show_view(self, view_name):
        """显示指定的视图"""
        if self.current_view:
            self.current_view.pack_forget()

        if view_name in self.views:
            # self.current_view.on_close()
            self.current_view = self.views[view_name]
            self.current_view.pack(fill=BOTH, expand=True)
            self.update_status(f"当前：{view_name}")

    def update_status(self, message, status_type="info"):
        """更新状态栏消息"""
        self.main_window.status_var.set(message)

        # 根据状态类型更改状态栏颜色
        if status_type == "error":
            self.main_window.status_bar.config(bootstyle=DANGER)
        elif status_type == "warning":
            self.main_window.status_bar.config(bootstyle=WARNING)
        else:
            self.main_window.status_bar.config(bootstyle=INFO)

    def _start_time_updater(self):
        """启动时间更新线程"""

        def update_time():
            while True:
                current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                self.main_window.time_var.set(current_time)
                time.sleep(1)

        thread = threading.Thread(target=update_time, daemon=True)
        thread.start()
